package dth.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.swing.plaf.synth.SynthTableHeaderUI;

import dth.dao.AccountDAO;
import dth.entity.Account;
import dth.entity.DthContact;
import dth.utils.DthSessionConstant;
import dth.utils.DthUtil;

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean {
	/*
	 * 1 is admin, 2 is dealer, 3 is distribute, 4 is user
	 */
	private int role;
	public static int roleCode = 0;
	public static int accountIdCode = 0;
	private int accountId;
	public static int id;
	private boolean logined;
	private String userName;
	private String password;
	private String invaildAccountMessage;
	
	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public static int getId() {
		return id;
	}

	public static void setId(int id) {
		LoginBean.id = id;
	}

	public String getInvaildAccountMessage() {
		return invaildAccountMessage;
	}

	public void setInvaildAccountMessage(String invaildAccountMessage) {
		this.invaildAccountMessage = invaildAccountMessage;
	}

	public String getUserName() {
		System.out.println("---get user name:");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		/*
		 * sign up back
		 */
		if(objectAction!=null)
		{
			System.out.println("---sign up succ");
			if(objectAction.equals("signsuccess"))
			DthUtil.showMessage("Message",
					"Your account has created. Login to use dth service");	
		}
		if (DthUtil.getAccountDTH() != null)
			return DthUtil.getAccountDTH().getUserName();
		return null;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		System.out.println("------get pass");
		invaildAccountMessage = "";
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAccountId() {
		return accountId;
	}

	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public boolean isLogined() {
		return logined;
	}

	public void setLogined(boolean logined) {
		this.logined = logined;
	}
	public void checkLogin() {
		AccountDAO accountDAO = new AccountDAO();
		System.out
				.println("--login username:" + userName + ",pass:" + password);
		if (accountDAO.checkLogin(userName, password) == null) {
			System.out.println("login fail");
			invaildAccountMessage = "(*)your account or password is invalid";
		} else {
			System.out.println("-----login success");
			Account account = accountDAO.checkLogin(userName, password);
			accountIdCode = account.getUserId();
			accountId = accountIdCode;

			accountId = account.getUserId();
			if (account.getFkAdminId() > 0) {
				role = 1;
				id = account.getFkAdminId();
				DthUtil.setSession(DthSessionConstant.DTH_USER, account);
				DthUtil.navigateTo("package.xhtml");
			} else if (account.getFkDealerId() > 0) {
				role = 2;
				id = account.getFkDealerId();
				DthUtil.setSession(DthSessionConstant.DTH_USER, account);
				DthUtil.navigateTo("welcome.xhtml");
			} else if (account.getFkDistributorId() > 0) {
				role = 3;
				id = account.getFkDistributorId();
				DthUtil.setSession(DthSessionConstant.DTH_USER, account);
				DthUtil.navigateTo("welcome.xhtml");
			} else if (account.getFkUserId() > 0) {
				role = 4;
				id = account.getFkUserId();
				DthUtil.setSession(DthSessionConstant.DTH_USER, account);
				DthUtil.navigateTo("welcome.xhtml");
			}
			roleCode = role;
			System.out.println("---account id:" + accountIdCode);
			System.out.println("----account name:" + account.getUserName());
			System.out.println("---id:"+id);
			System.out.println("----role:" + role);
			System.out.println("----acount session name:"
					+ DthUtil.getAccountDTH().getUserName());
			logined = true;

		}
	}

	public void logOut() {
		DthUtil.removeSession(DthSessionConstant.DTH_USER);
		logined = false;
		invaildAccountMessage = "";
		roleCode = 0;
		role = 0;
		id = 0;
		DthUtil.navigateTo("welcome.xhtml");
	}

	public void signUp() {
		DthUtil.navigateTo("register.xhtml");
	}

}
